package com.bdqn.controller;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.entity.PsUser;
import com.bdqn.service.IPsUserService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dede
 * @since 2020-05-23
 */
@RestController
public class PsUserController {
	@Autowired
	private IPsUserService psUserService;

	@GetMapping("index")
	public List<PsUser> index() {
		return psUserService.list();
	}

	@GetMapping("asdf")
	public IPage<PsUser> asdf(Integer page) {
		return psUserService.page(new Page<PsUser>(page == null ? 1 : page, 3));
	}

	@GetMapping("qwer")
	public IPage<PsUser> qwer(Integer un, Integer rn, Integer pageNum, Integer pageSize) {
		return psUserService.qwer(un, rn, pageNum == null ? 1 : pageNum, 2);
	}

	@RequestMapping("add")
	public String add(@RequestParam("name") String name) {
		System.out.println(name);
		return null;
	}
	
	@RequestMapping("adduser")
	public boolean adduser(@RequestParam(name="usercode",required=false)String usercode,
			               @RequestParam(name="username",required=false)String username,
			               @RequestParam(name="userpassword",required=false)String userpassword,
			               @RequestParam(name="sex",required=false)String sex,
			               @RequestParam(name="age",required=false)Integer age,
			               @RequestParam(name="entrydate",required=false)String entrydate,
			               @RequestParam(name="phonetel",required=false)String phonetel,
			               @RequestParam(name="email",required=false)String email,
			               @RequestParam(name="createdby",required=false)String createdby) {
		PsUser puser=new PsUser();
		puser.setUsercode(usercode);
		puser.setUsername(username);
		puser.setUserpassword(userpassword);
		puser.setSex(sex);
		puser.setAge(age);
		puser.setEntrydate(entrydate);
		puser.setPhonetel(phonetel);
		puser.setEmail(email);
		puser.setCreationtime(LocalDateTime.now());
		puser.setLastlogintime(LocalDateTime.now());
		puser.setCreatedby(createdby);
		puser.setLastupdatetime(LocalDateTime.now());
		return psUserService.save(puser);
	}
	
	@RequestMapping("altuser")
	public boolean altuser(@RequestParam(name="id",required=false)Integer id,
			               @RequestParam(name="usercode",required=false)String usercode,
			               @RequestParam(name="username",required=false)String username,
			               @RequestParam(name="userpassword",required=false)String userpassword,
			               @RequestParam(name="sex",required=false)String sex,
			               @RequestParam(name="age",required=false)Integer age,
			               @RequestParam(name="entrydate",required=false)String entrydate,
			               @RequestParam(name="dimissiondate",required=false)String dimissiondate,
			               @RequestParam(name="phonetel",required=false)String phonetel,
			               @RequestParam(name="email",required=false)String email) {
		PsUser pusera=new PsUser();
		pusera.setUsercode(usercode);
		pusera.setUsername(username);
		pusera.setUserpassword(userpassword);
		pusera.setSex(sex);
		pusera.setAge(age);
		pusera.setEntrydate(entrydate);
		pusera.setDimissiondate(dimissiondate);
		pusera.setPhonetel(phonetel);
		pusera.setEmail(email);
		pusera.setLastlogintime(LocalDateTime.now());
		pusera.setLastupdatetime(LocalDateTime.now());
		return psUserService.save(pusera);
	}
}
